﻿using System;
using TrainSimulator.Interfaces;

namespace TrainSimulator.State
{
    internal class TrainBreaking : State
    {
        public TrainBreaking()
        {
            base._description = "Breaking";
        }

        public override Interfaces.IState Handle(ITrain train)
        {
            IState stateResult = null;
            if (train.Speed > 0.2)
            {
                double accelerating = train.Speed / 2;
                if (train.Engine.Deacceleration < accelerating)
                    accelerating = -train.Engine.Deacceleration;
                else
                    accelerating = -accelerating;
                train.Engine.Accelerating = accelerating;
                stateResult = this;
            }
            else
            {
                train.Engine.Accelerating = 0;
                train.Speed = 0;
                stateResult = train.PreviousState;
            }
            return stateResult;
        }
    }
}
